package cn.bdqn.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;






import cn.bdqn.entity.BackendUser;
import cn.bdqn.service.backendUserService;


@Controller
@RequestMapping("/backend")
public class backUserController {
	@Autowired
	private backendUserService backendUserService;
	@RequestMapping("/disLogin")
	public String doLogin() throws Exception{
		return "/backendLogin";
	}
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpSession session,
			@RequestParam(value="backCode",required=false)String username,
			@RequestParam(value="backPassword",required=false)String password)
					throws Exception{
		//掉service层 登录方法
	BackendUser backendUser=backendUserService.login(username,password);
		if(backendUser==null){
			//loser
			request.setAttribute("loginInfo", "用户名或密码错误...");
			return "backendLogin";
		}else{
			//Nice 1,先保存数据到session 2，跳转到main
			session.setAttribute("UserSession", backendUser);
			return "redirect:/backendApp/main";
		}
	}
	
	/**
	 * 退出
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) throws Exception{
		//清除session
		session.removeAttribute("UserSession");
		return "/backendLogin";
	}
	
	
}
